package com.corgi.formatter;

import lombok.Data;

import java.io.*;
import java.text.MessageFormat;

/**
 * @title: Test
 * @description:
 * @author: dengmiao
 * @create: 2019-10-11 10:23
 **/
public class TestSerializer {

	public static void main(String[] args) throws Exception {
		//序列化Person对象
		SerializePerson();
		//反序列Perons对象
		Person p = DeserializePerson();
		System.out.println(MessageFormat.format("name={0},age={1},sex={2}",
				p.getName(), p.getAge(), p.getSex()));
	}

	/**
	 * MethodName: SerializePerson
	 * Description: 序列化Person对象
	 * @author xudp
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	private static void SerializePerson() throws FileNotFoundException,
			IOException {
		Person person = new Person();
		person.setName("gacl");
		person.setAge(25);
		person.setSex("男");
		// ObjectOutputStream 对象输出流，将Person对象存储到E盘的Person.txt文件中，完成对Person对象的序列化操作
		ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(
				new File("F:/Person.txt")));
		oo.writeObject(person);
		System.out.println("Person对象序列化成功！");
		oo.close();
	}

	/**
	 * MethodName: DeserializePerson
	 * Description: 反序列Perons对象
	 * @author xudp
	 * @return
	 * @throws Exception
	 * @throws IOException
	 */
	private static Person DeserializePerson() throws Exception, IOException {
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
				new File("F:/Person.txt")));
		Person person = (Person) ois.readObject();
		System.out.println("Person对象反序列化成功！");
		return person;
	}

	@Data
	private static class Person implements Serializable {
		private static final long serialVersionUID = -1L;
		private int age;
		private String name;
		private String sex;
	}
}
